package file

import (
	"github.com/gin-gonic/gin"
	"github.com/vueadmin/app/admin/internal/dto"
	"github.com/vueadmin/app/admin/internal/service"
	"github.com/vueadmin/app/admin/internal/svc"
	"github.com/vueadmin/utils"
	"github.com/vueadmin/utils/common/request"
	"github.com/vueadmin/utils/common/result"
	"github.com/vueadmin/utils/response"
)

// 数据列表
func GetPageList(c *gin.Context, svcCtx *svc.ServiceContext) {
	req := dto.FilePageReq{
		PageReq: request.PageReq{Page: 1, Limit: 20},
	}
	if err := utils.BindAndValid(c, &req); err != nil {
		response.Fail(c, err.Error())
		return
	}
	s := service.NewFileService(svcCtx)
	list, count, err := s.GetPageList(&req)
	if err != nil {
		response.Err(c, err)
		return
	}
	response.Success(c, "返回成功", result.M{
		"data":  list,
		"total": count,
	})
}

// 创建
func Add(c *gin.Context, svcCtx *svc.ServiceContext) {
	req := dto.FileAddReq{}
	if err := utils.BindAndValid(c, &req); err != nil {
		response.Fail(c, err.Error())
		return
	}
	s := service.NewFileService(svcCtx)
	ret, err := s.Add(&req)
	if err != nil {
		response.Err(c, err)
		return
	}
	response.Success(c, "添加成功", ret)
}

// 删除
func Delete(c *gin.Context, svcCtx *svc.ServiceContext) {
	req := dto.FileDeleteReq{}
	if err := utils.BindAndValid(c, &req); err != nil {
		response.Fail(c, err.Error())
		return
	}
	s := service.NewFileService(svcCtx)
	if err := s.Delete(&req); err != nil {
		response.Err(c, err)
		return
	}
	response.Success(c, "删除成功", nil)
}
